<?php
/**
 * Created by PhpStorm.
 * User: tingyugetc
 * Date: 16/12/21
 * Time: 下午9:18
 */

namespace App\Repositories;


use App\Models\Trade;

class TradeRepository
{
    use BaseRepository;

    protected $model;

    public function __construct(Trade $trade) {
        $this->model = $trade;
    }

    public function lists($pageSize) {
        return $this->model->orderBy('id', 'desc')->paginate($pageSize);
    }

    public function store($input)
    {
        return $this->savePost($this->model, $input);
    }

    public function update($tradeId, $input) {
        $model = $this->model->find($tradeId);
        return $this->savePost($model, $input);
    }

    public function storeLogo($tradeId, $logoUrl) {
        $trade = Trade::find($tradeId);
        $trade->logo = $logoUrl;
        $trade->save();
        return $trade;
    }

    public function savePost(Trade $trade, $input) {
        $trade->fill($input->all());

        $trade->save();
        return $trade;
    }

    public function find($tradeId) {
        return Trade::find($tradeId);
    }
}